<template>
	<view>
		<!-- 巡检点 -->
		<view class="inspection_io">
			<scroll-view scroll-y="true" class="scroll-Y">
				<view class="inspection_header">
					<view class="inspection_text">
						<text>巡检设备:</text>
						<text class="text_center">{{ isForm(inspectionForm.deviceType)}}</text>
					</view>
					<view class="inspection_text">
						<text>巡检内容:</text>
						<text class="text_center">{{inspectionForm.inspectionContent}}</text>
					</view>
				</view>

				<view class="inspection_center">
					<view class="">
						<view class="inspection_text">
							现场照片
						</view>

						<view>
							<uni-file-picker limit="9" @select="selectX" v-model="imageValueX"></uni-file-picker>
						</view>
					</view>
					<view class="">
						<view class="inspection_text">
							巡检情况
						</view>
						<view>
							<uni-easyinput type="textarea" autoHeight v-model="easyinputValue"
								placeholder="请输入内容"></uni-easyinput>
						</view>
					</view>
					<view class="">
						<view class="inspection_text">
							运维内容
						</view>
						<view>
							<uni-easyinput type="textarea" autoHeight v-model="operationValue"
								placeholder="请输入内容"></uni-easyinput>
						</view>
					</view>
					<view class="">
						<view class="inspection_text">
							是否有隐患
						</view>
						<uni-data-select v-model="pitfallValue" :localdata="pitfallRange"></uni-data-select>
						</uni-section>
					</view>


		

					<view class="" v-if="pitfallValue=='Y'">
						<view class="">
							<view class="inspection_text">
								隐患整改进度
							</view>
							<uni-data-select v-model="rectificationValue"
								:localdata="rectificationRange"></uni-data-select>
							</uni-section>
						</view>
						<view v-if="rectificationValue == 'Y'" class="">
							<view class="inspection_text">
								已采取措施
							</view>
							<view>
								<uni-easyinput type="textarea" v-model="easyinputCuos"
									placeholder="请输入内容"></uni-easyinput>
							</view>
						</view>
						<view v-if="rectificationValue == 'Y'" class="">
							<view class="inspection_text">
								整改照片
							</view>

							<view>
								<uni-file-picker @select="selectZ" v-model="imageValueZ" limit="9"></uni-file-picker>
							</view>
						</view>
					</view>
				</view>

				<view class="inspection_bottom">
					<view class="bottom_btn" @tap="bottomBtn()">
						保存
					</view>
				</view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	import config from '@/config'
	import {
		putRecord,
		getEtInfo
	} from '@/apis/modules/inspectionApp';
	import {
		getDicts
	} from '@/apis/modules/dict';
	import {
		getToken
	} from '@/utils/auth'
	import QQMapWX from "@/common/qqmap-wx-jssdk.min.js";
	import projectConfig from '@/env.config.js';
	export default {
		data() {
			return {
				baseUrl: projectConfig.baseUrl,
				inspectionData: [{
					title: '息县污水站巡检',
					jihua: '每周二,周五',
					name: '装完',
					status: 1,
					img: [
						'https://zy.yunxinhai.com/prod-api/profile/upload/2024/08/29/774a2a7f751d76028a016092b83c5b5471116664af95-gZb66Z_fw1200_20240829132100A010.jpg'
					]
				}],
				pitfallRange: [{
						value: 'N',
						text: "无隐患"
					},
					{
						value: 'Y',
						text: "有隐患"
					},
				],
				rectificationRange: [{
						value: 'N',
						text: "未整改"
					},
					{
						value: 'Y',
						text: "已整改"
					},
				],
				pitfallValue: '',
				easyinputValue: '',
				rectificationValue: '',
				easyinputCuos: '',
				operationValue:'',
				imageValueZ: [],
				imageValueX: [],
				id: '',
				isWeiz1: '',
				inspectionForm: {
					deviceType: '',
					inspectionContent: ''
				},
				longitude: '',
				latitude: '',
				signInAddr: '',
				timesData: '',
				planId: '',
				getEtInfoForm: {},
				getDicts: []
			}
		},
		onLoad(options) {
			console.log(options.id)
			this.id = options.id;
			this.planId = options.planId;
			this.setId = options.setId;
			this.inspectionRecordId = options.inspectionRecordId
			this.getMyTaskRecordListIdFun(options.id)
			this.handleLocation()
			this.isData();
		},
		methods: {
			// 字典
			isData() {
				getDicts('associated_devices').then(res => {
					this.getDicts = res.data
				})
			},
			isForm(e) {
				for (let item of this.getDicts) {
					if (item.dictValue == e) {
						return item.dictLabel
					}
				}
			},
			getMyTaskRecordListIdFun(id) {
				getEtInfo(id).then((res) => {
					console.log(res)
					this.inspectionForm = res.data
				})
			},
			bottomBtn() {

				let img1 = [];
				for (let item of this.imageValueX) {
					img1.push(item.imgs)
				}

				let img2 = [];
				for (let item of this.imageValueZ) {
					img2.push(item.imgs)
				}

				let parms = {
					sceneImgs: img1.toString(), //现场照片 img1
					pitfall: this.pitfallValue, //是否有隐患
					resultContent: this.easyinputValue, //巡检情况
					pitfallCorrection: this.rectificationValue, //是否整改
					pitfallCorrectionContent: this.easyinputCuos, //已采取措施
					pitfallCorrectionImgs: img2.toString(), //整改照片 img2
					inspectionRecordId: this.inspectionRecordId, //扫码的id
					inspectionSettingId: this.id, //设备点id
					inspectionId: this.setId,
					signInAddr: this.signInAddr,
					signInLngLat: this.longitude + "," + this.latitude,
					// signOutLngLat: this.longitude,
				}
				putRecord(parms).then((res) => {
					console.log(res)
					if (res.code == 200) {
						// uni.navigateTo({
						// 	url: `/pagesB/inspectionApp/InspectionCode?id=${this.planId}&times=${this.timesData}`
						// });
						uni.navigateBack();
					}
				})

			},
			todayDate() {
				const now = new Date();
				const year = now.getFullYear();
				const month = (now.getMonth() + 1).toString().padStart(2, '0');
				const day = now.getDate().toString().padStart(2, '0');
				this.timesData = `${year}-${month}-${day}`
			},
			selectX(e) {
				uni.uploadFile({
					url: `${this.baseUrl}/iot/tool/upload`, //仅为示例，非真实的接口地址
					filePath: e.tempFilePaths[0],
					name: 'file',
					header: {
						Authorization: 'Bearer ' + this.$store.state.vuex_token
					},
					success: (uploadFileRes) => {
						let resArrayJson = JSON.parse(
							uploadFileRes.data)
						this.imageValueX.push({
							url: this.baseUrl + resArrayJson.url,
							fileName: this.baseUrl + resArrayJson.fileName,
							imgs: resArrayJson.url
						})
						console.log(this.imageValueX, 'this.imageValueX')
					},
					fail: (rrr) => {
						console.log("失败", rrr)
					}
				});

			},
			selectZ(e) {
				uni.uploadFile({
					url: `${this.baseUrl}/iot/tool/upload`, //仅为示例，非真实的接口地址
					filePath: e.tempFilePaths[0],
					name: 'file',
					header: {
						Authorization: 'Bearer ' + this.$store.state.vuex_token
					},
					success: (uploadFileRes) => {
						let resArrayJson = JSON.parse(
							uploadFileRes.data)
						this.imageValueZ.push({
							url: this.baseUrl + resArrayJson.url,
							fileName: this.baseUrl + resArrayJson.fileName,
							imgs: resArrayJson.url
						})
						console.log(this.imageValueZ, 'this.imageValueZ')
					},
					fail: (rrr) => {
						console.log("失败", rrr)
					}
				});
			},
			// 点击界面获取定位
			async handleLocation() {
				// 权限问题，app 平台需要判断
				// #ifdef APP-PLUS
				const location = uni.getStorageSync('location');
				if (!location) {
					let result = await this.$store.dispatch("permission/requestPermissions", 'ACCESS_FINE_LOCATION');
					if (result !== 1) return;
					uni.setStorageSync('location', true);
				}
				// #endif
				// 其他平台直接获取定位
				this.getLocationFun();
			},
			// 获取定位查询天气
			getLocationFun() {
				console.log('获取定位');
				let _this = this
				uni.getLocation({
					type: 'wgs84',
					success(res) {
						console.log(111111111, res)
						_this.longitude = res.longitude;
						_this.latitude = res.latitude;
						_this.adressFun()

					},
					fail(e) {
						console.log(e);
						uni.showToast({
							title: '无法获取定位',
							icon: 'none'
						});
					}
				});
			},
			adressFun() {
				let _this = this
				// 实例化API核心类
				let qqmapsdk = new QQMapWX({
					key: projectConfig.qqmapKey // 必填
				});

				// 使用逆地理编码服务
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: _this.latitude,
						longitude: _this.longitude
					},
					success: function(res) {
						// 服务调用成功后的处理函数
						console.log(res.result.address);
						_this.signInAddr = res.result.address
					},
					fail: function(error) {
						// 服务调用失败的处理函数
						console.error(error);
					}
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.inspection_io {
		width: 100%;
	}

	.scroll-Y {
		height: calc(100vh - 210rpx);
	}

	.inspection_text {
		font-size: 30rpx;
		padding: 20rpx 0;
		font-weight: bold;
	}

	.text_center {
		padding-left: 20rpx;
		font-weight: 400;
	}

	.inspection_text1 {
		font-size: 26rpx;
		color: #9e9e9e;
	}

	.inspection_header {
		background-color: white;
		border-radius: 10rpx;
		padding: 20rpx;
	}

	.inspection_center {
		background-color: white;
		border-radius: 10rpx;
		padding: 0 20rpx 20rpx 20rpx;
		// margin-top: 10rpx;
	}

	.inspection_bottom {
		position: fixed;
		height: 120rpx;
		width: 100%;
		bottom: 0;
		left: 0;

		.bottom_btn {
			width: 85%;
			margin: auto;
			background-color: #007AFC;
			color: white;
			text-align: center;
			padding: 20rpx 0;
			border-radius: 30rpx;
		}
	}
	
		::v-deep .uni-select{
			z-index: 999;
		}
</style>